package org.jeecg.modules.room.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.modules.room.vo.RoomFpStatusVo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.room.entity.RoomBookInfoMx;
import org.jeecg.modules.room.entity.RoomBookInfo;
import org.jeecg.modules.room.vo.RoomBookInfoVo;
import org.jeecg.modules.room.service.IRoomBookInfoService;
import org.jeecg.modules.room.service.IRoomBookInfoMxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 预定单主信息
 * @Author: jeecg-boot
 * @Date:   2023-12-19
 * @Version: V1.0
 */
@Api(tags="预定单主信息")
@RestController
@RequestMapping("/room/roomBookInfo")
@Slf4j
public class RoomBookInfoController {
	@Autowired
	private IRoomBookInfoService roomBookInfoService;
	@Autowired
	private IRoomBookInfoMxService roomBookInfoMxService;
	
	/**
	 * 分页列表查询
	 *
	 * @param roomBookInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="预定单主信息-分页列表查询", notes="预定单主信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoomBookInfo>> queryPageList(RoomBookInfo roomBookInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoomBookInfo> queryWrapper = QueryGenerator.initQueryWrapper(roomBookInfo, req.getParameterMap());
		Page<RoomBookInfo> page = new Page<RoomBookInfo>(pageNo, pageSize);
		IPage<RoomBookInfo> pageList = roomBookInfoService.selectBookInfoList(page, queryWrapper);
		for(RoomBookInfo bookInfo : pageList.getRecords()){
			List<RoomFpStatusVo> roomFpStatusVoList = roomBookInfoService.selectFpStatus(bookInfo.getBookCode());
			String roomFpDesc = "",roomFpCode = "";
			for(RoomFpStatusVo roomFpStatusVo : roomFpStatusVoList){
				roomFpDesc = roomFpDesc + roomFpStatusVo.getRoomTypeName()+"("+roomFpStatusVo.getFpRoomNum().toString() + "/" + roomFpStatusVo.getBookRoomNum() +")【"+ roomFpStatusVo.getRoomCodes() + "】" + "<br/>";
				roomFpCode = roomFpCode + roomFpStatusVo.getRoomCodes()+"<br/>";
			}
			bookInfo.setRoomFpDesc(roomFpDesc);
			bookInfo.setRoomFpCode(roomFpCode);
		}
		return Result.OK(pageList);
	}

	 @ApiOperation(value="预定房间分配情况", notes="预定房间分配情况")
	 @GetMapping(value = "/queryBookByDate")
	 public Result<Object> queryBookByDate(){
		return Result.OK(roomBookInfoService.selectBookByDate());
	 }
	
	/**
	 *   添加
	 *
	 * @param roomBookInfoVo
	 * @return
	 */
	@AutoLog(value = "预定单主信息-添加")
	@ApiOperation(value="预定单主信息-添加", notes="预定单主信息-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoomBookInfoVo roomBookInfoVo) {
		RoomBookInfo roomBookInfo = new RoomBookInfo();
		BeanUtils.copyProperties(roomBookInfoVo, roomBookInfo);
		roomBookInfoService.saveMain(roomBookInfo, roomBookInfoVo.getRoomBookInfoMxList());
		return Result.OK("添加成功！");
	}

	 /**
	  *   确认订单
	  *
	  * @param bookCodes
	  * @return
	  */
	 @PostMapping(value = "/confirmBook")
	 public Result<String> confirmBook(@RequestParam(name="bookCodes",required=true) String bookCodes) {
		 roomBookInfoService.confirmBook(bookCodes);
		 return Result.OK("操作成功！");
	 }

	 /**
	  *   撤销确认订单
	  *
	  * @param bookCodes
	  * @return
	  */
	 @PostMapping(value = "/unConfirmBook")
	 public Result<String> unConfirmBook(@RequestParam(name="bookCodes",required=true) String bookCodes) {
		 roomBookInfoService.unConfirmBook(bookCodes);
		 return Result.OK("操作成功！");
	 }


	 /**
	  *   取消订单
	  *
	  * @param bookCodes
	  * @return
	  */
	 @PostMapping(value = "/cancelBook")
	 public Result<String> cancelBook(@RequestParam(name="bookCodes",required=true) String bookCodes) {
		 roomBookInfoService.cancelBook(bookCodes);
		 return Result.OK("取消成功！");
	 }


	/**
	 *  编辑
	 *
	 * @param roomBookInfoVo
	 * @return
	 */
	@AutoLog(value = "预定单主信息-编辑")
	@ApiOperation(value="预定单主信息-编辑", notes="预定单主信息-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoomBookInfoVo roomBookInfoVo) {
		RoomBookInfo roomBookInfo = new RoomBookInfo();
		BeanUtils.copyProperties(roomBookInfoVo, roomBookInfo);
		RoomBookInfo roomBookInfoEntity = roomBookInfoService.getById(roomBookInfo.getId());
		if(roomBookInfoEntity==null) {
			return Result.error("未找到对应数据");
		}
		else if(!roomBookInfoEntity.getBookStatus().equals("00")){
			return Result.error("非未确认状态不可编辑!");
		}
		roomBookInfoService.updateMain(roomBookInfo, roomBookInfoVo.getRoomBookInfoMxList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "预定单主信息-通过id删除")
	@ApiOperation(value="预定单主信息-通过id删除", notes="预定单主信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		roomBookInfoService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "预定单主信息-批量删除")
	@ApiOperation(value="预定单主信息-批量删除", notes="预定单主信息-批量删除")
    //@RequiresPermissions("org.jeecg.modules:room_book_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.roomBookInfoService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="预定单主信息-通过id查询", notes="预定单主信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RoomBookInfo> queryById(@RequestParam(name="id",required=true) String id) {
		RoomBookInfo roomBookInfo = roomBookInfoService.getById(id);
		if(roomBookInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roomBookInfo);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="预订单明细信息主表ID查询", notes="预订单明细信息-通主表ID查询")
	@GetMapping(value = "/queryRoomBookInfoMxByMainId")
	public Result<IPage<RoomBookInfoMx>> queryRoomBookInfoMxListByMainId(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																		@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																		@RequestParam(name="id",required=true) String id) {
		Page<RoomBookInfoMx> page = new Page<RoomBookInfoMx>(pageNo, pageSize);
		IPage<RoomBookInfoMx> roomBookInfoMxList = roomBookInfoMxService.selectByMainId(page,id);
		return Result.OK(roomBookInfoMxList);
	}

}
